﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using AutoPartsSearch.Domain.Abstract;
using AutoPartsSearch.Domain.Abstract.Repositories;
using AutoPartsSearch.Domain.Common;
using AutoPartsSearch.Domain.Entities;

namespace AutoPartsSearch.Domain.Concrete.Repositories
{
    public class DapperOfferRepository : BaseRepository, IOfferRepository
    {
        public DapperOfferRepository(ILanguageGetter languageGetter)
            : base(languageGetter)
        {
        }

        public IEnumerable<Entities.Offer> GetBySeller(int sellerId, int page, int pageSize, out int totalCount)
        {
            var total = 0;
            var param = new Dapper.DynamicParameters();
            param.Add("@SellerId", sellerId, DbType.Int32);
            param.Add("@Page", page, DbType.Int32);
            param.Add("@PageSize", pageSize, DbType.Int32);
            param.Add("@Lang", this.CurrentLanguage, DbType.String);
            param.Add("@TotalCount", dbType: DbType.Int32, direction: ParameterDirection.Output);
            var result = QueryExecutor.QueryDynamic<Offer>("sp_GetOffersBySeller", ModelReader, param).ToList();
            totalCount = param.Get<int>("@TotalCount");
            return result;
        }

        public int Add(Entities.Offer instance)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@Id", dbType: DbType.Int32, direction: ParameterDirection.Output);
            param.Add("@SellerId", instance.SellerId, DbType.Int32);
            param.Add("@QueryId", instance.QueryId, DbType.Int32);
            param.Add("@Price", instance.Price, DbType.Decimal);
            param.Add("@Currency", instance.Currency, DbType.String);
            param.Add("@Comments", instance.Comments, DbType.String);
            param.Add("@Created", DateTime.Now, DbType.Date);
            QueryExecutor.ExecuteProcedure("sp_AddOffer", param);
            return param.Get<int>("@Id");
        }

        public Offer GetByIdAndSeller(int id, int sellerId)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@Id", id, DbType.Int32);
            param.Add("@SellerId", sellerId, DbType.Int32);
            param.Add("@Lang", this.CurrentLanguage, DbType.String);
            return QueryExecutor.QueryDynamic<Offer>("sp_GetOfferByIdAndSeller", ModelReader, param)
                .FirstOrDefault();
        }

        public void Update(Entities.Offer instance)
        {
            throw new NotImplementedException();
        }

        public Entities.Offer GetById(int id)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<Entities.Offer> Get()
        {
            throw new NotImplementedException();
        }

        public IEnumerable<Entities.Offer> Get(int page, int pageSize)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<Offer> GetOffersByQuery(int queryId)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@QueryId", queryId, DbType.Int32);
            return QueryExecutor.QueryDynamic<Offer>("sp_GetOffersByQuery", SimpleModelReader, param).ToList();
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        public long GetTotalCount()
        {
            throw new NotImplementedException();
        }

        private void SimpleModelReader(Offer offer, dynamic o)
        {
            offer.SellerId = o.SellerId;
            offer.QueryId = o.QueryId;
            offer.Id = o.Id;
            offer.Price = o.Price;
            offer.Currency = o.Currency;
            offer.Created = Converter.ConvertToMoldTime(o.Created);
        }

        private void ModelReader(Offer offer, dynamic o)
        {
            offer.SellerId = o.SellerID;
            offer.QueryId = o.QueryID;
            offer.Id = o.ID;
            offer.Price = o.Price;
            offer.Currency = o.Currency;
            offer.Created = o.Created;

            offer.SearchQuery = new SearchQuery
            {
                Id = o.QueryID,
                PartName = o.PartName,
                PartGroup = new Dictionary(o.PartGroup),
                PartType = new Dictionary(o.PartType),
                PartCatalogNumber = o.PartCatalogNumber,
                PartState = new Dictionary(o.PartState),
                PartDescription = o.PartDescription,
                CarType = new Dictionary(o.CarType),
                CarYear = o.CarYear,
                CarBrand = new Dictionary(o.CarBrand),
                CarModel = o.CarModel,
                CarModification = o.CarModification,
                CarVin = o.CarVin,
                CarEngine = o.CarEngine,
                CarCase = new Dictionary(o.CarCase),
                CarTransmission = new Dictionary(o.CarTransmission),
                CarDrive = new Dictionary(o.CarDrive),
                CarFuelType = new Dictionary(o.CarFuelType),
                City = new Dictionary(o.City),
                Name = o.PartName,
                Phone = o.Phone,
                Phone2 = o.Phone2,
                Email = o.Email,
                Comment = o.QueryComment,
                Created = o.QueryCreated,
            };
        }





        
    }
}
